package com.sun.hunk.hibernate.domain.impl;

import com.sun.hunk.hibernate.domain.Link;
import com.sun.hunk.hibernate.domain.LinkTree;
import com.sun.hunk.hibernate.domain.RoleLink;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author sunil
 */
@Entity
@Table(name = "link_tree")
public class LinkTreeImpl implements LinkTree {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @OneToMany(mappedBy = "linkTree",targetEntity=RoleLinkImpl.class)
    private Set<RoleLink> roleLinkSet;
    @JoinColumn(name = "item_id", referencedColumnName = "id")
    @ManyToOne(targetEntity=LinkImpl.class)
    private Link child;
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @ManyToOne(targetEntity=LinkImpl.class)
    private Link parent;
    @Column
    private Integer index;

    public LinkTreeImpl() {
    }

    public LinkTreeImpl(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public Set<RoleLink> getRoleLinkSet() {
        return roleLinkSet;
    }

    public void setRoleLinkSet(Set<RoleLink> roleLinkSet) {
        this.roleLinkSet = roleLinkSet;
    }

    public Link getChild() {
        return child;
    }

    public void setChild(Link child) {
        this.child = child;
    }

    public Link getParent() {
        return parent;
    }

    public void setParent(Link parent) {
        this.parent = parent;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof LinkTreeImpl)) {
            return false;
        }
        LinkTreeImpl other = (LinkTreeImpl) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.cong.logiware.hibernate.domain.LinkTree[id=" + id + "]";
    }

    @Override
    public int compareTo(LinkTree tree) {
        if(tree == null){
            return -1;
        }
        int diff = (int) (parent.getId() - tree.getParent().getId());

        if(diff != 0 ){
            return diff;
        }
        return diff = index - tree.getIndex();
    }

}
